layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'util', 'form'], function () {
        lui = layui;
        $ = layui.$;
        let util = layui.util;

        table = layui.table;
        table.render({
            elem: '#dataTable'
            , url: '/system/getDataBackup'
            , contentType: 'application/json'
            , method: 'post'
            , cellMinWidth: 80
            , page: true
            , limit: 50
            , height: 'full-65'
            , loading: true
            , where: {model: "1"}
            , size: 'sm'
            , cols: [
                [ //标题栏
                    {field: 'status', title: '状态', width: 60, templet: '#StatusTpl'},
                    {field: 'createTime', title: '创建时间', width: 140},
                    {field: 'creater', title: '创建人', width: 100},
                    {field: 'executeTime', title: '执行时间', width: 140},
                    {field: 'executer', title: '执行人', width: 100},
                    {field: 'takeTime', title: '执行用时', width: 80},
                    {field: 'fileName', title: '文件名', width: 150},
                    {field: 'info', title: '备份信息'},
                    {field: 'downNum', title: '下载次数', width: 100},
                    {fixed: 'right', title: '操作', width: 60, toolbar: '#operationBar'}
                ]
            ],
        });

        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'execute') {
                top.layer.confirm('确定要执行当前选择的备份任务吗？ ', function (index) {
                    $.ajax({
                        url: '../system/executeDataBackup', //url
                        dataType: 'json', //数据格式
                        type: 'POST', //请求方式
                        async: false, //是否异步请求
                        data: {id: data.id, status: 1},
                        success: function (result) {
                            if (result.code == 0) {
                                table.reload('dataTable');
                                layer.close(index);
                                top.parent.layer.msg(result.message, {icon: 1});
                            } else {
                                top.parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    return false;
                });
            } else if (obj.event === 'download') {
                let url = "../system/downloadDataBackup?id=" + data.id;
                download(url);
                return false;
            }
        });

        util.on("lay-event", {
            createDataBackup: function () {
                top.layer.confirm('确定要创建数据备份任务吗？ ', function (index) {
                    $.ajax({
                        url: '../system/createDataBackup', //url
                        dataType: 'json', //数据格式
                        type: 'POST', //请求方式
                        async: false, //是否异步请求
                        data: {model: "1"},
                        success: function (result) {
                            if (result.code == 0) {
                                table.reload('dataTable');
                                layer.close(index);
                                top.parent.layer.msg(result.message, {icon: 1});
                            } else {
                                top.parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    return false;
                });
            }

        });

        function downloadFile(url, fileName) {
            console.log("ajaxDownloadSynchronized");
            var url = url;
            var fileName = fileName;
            var form = $("<form></form>").attr("action", url).attr("method", "post");
            form.append($("<input></input>").attr("type", "hidden").attr("name", "fileName").attr("value", fileName));
            form.appendTo('body').submit().remove();
        }

        function download(url) {
            try {
                var elemIF = document.createElement("iframe");
                elemIF.src = url;
                elemIF.style.display = "none";
                document.body.appendChild(elemIF);
            } catch (e) {
                alert("下载异常！");
            }
        }

    });


